package com.passengerflowmanagementsystem.statistics.utils;

import java.util.ArrayList;
import java.util.List;

public class ByteArrayConveter {


    public static List<Float> getFloatArray(int[] arr) {
        List<Float> floatList = new ArrayList<>();
        int[] newArr = new int[2];
        for (int i = 0; i < arr.length; i += 2) {
            //需要0的话可以不去掉
            if (arr[i] == 0) {
                continue;
            }
            newArr[0] = arr[i];
            newArr[1] = arr[i + 1];
            byte[] bytes = toByteArray(newArr);
            floatList.add(getFloat(bytes, 0));

        }
        return floatList;
    }

    public static byte[] toByteArray(int[] i16) {
        byte[] dst = new byte[i16.length * 2];
        int i = 0;

        for (int j = 0; i < i16.length; j += 2) {
            dst[j] = (byte)(i16[i] >> 8 & 255);
            dst[j + 1] = (byte)(i16[i] & 255);
            ++i;
        }

        return dst;
    }

    public static float getFloat(byte[] arr, int index) {
        return Float.intBitsToFloat(getInt(arr, index));

    }

    // 从byte数组的index处的连续4个字节获得一个int

    public static int getInt(byte[] arr, int index) {
        return (0xff000000 & (arr[index + 0] << 24)) |

                (0x00ff0000 & (arr[index + 1] << 16)) |

                (0x0000ff00 & (arr[index + 2] << 8)) |

                (0x000000ff & arr[index + 3]);

    }

}
